package com.csxy.six.service.impl;
// MaterialServiceImpl.java



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.six.mapper.MaterialMapper;
import com.csxy.six.entity.Material;
import com.csxy.six.service.MaterialService;
import com.csxy.six.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



import java.util.List;


@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Autowired
    private OssService ossService;

    @Override
    public List<Material> getMaterialsByTag(String tag) {
        return lambdaQuery()
                .like(Material::getTags, tag)
                .list();
    }

    @Override
    @Transactional
    public boolean downloadMaterial(Long materialId, Long userId) {
        Material material = getById(materialId);
        if (material == null) {
            return false;
        }

        // 检查用户权限（简化为示例）
        if (!hasDownloadPermission(userId, materialId)) {
            return false;
        }

        // 更新下载次数
        material.setDownloadCount(material.getDownloadCount() + 1);
        updateById(material);
        return true;
    }

    private boolean hasDownloadPermission(Long userId, Long materialId) {
        // 实际项目中应调用用户服务检查角色
        return true; // 简化为允许所有登录用户
    }
}